<template>
    <view class="bg-content">
        <div style="background-color: white; padding: 15px;">
            <u-form ref="validateFormRef" :model="validateForm.value" :label-width="300">
                <u-form-item :label-width="400" label="是否显示广告" prop="isShowAd">
                    <u-switch active-color="red" v-model="validateForm.value.isShowAd" slot="right"></u-switch>
                </u-form-item>

                <u-form-item label="广告高度" prop="adHeight">
                    <u-input v-model="validateForm.value.adHeight" placeholder="请输入广告高度"/>
                </u-form-item>
                <adIndex ref="adIndexRef" :fkId="objParams.id"></adIndex>

                <u-form-item :label-width="400" label="是否显示二级系统分类" prop="isShowSystemClassLevel2">
                    <u-switch active-color="red" v-model="validateForm.value.isShowSystemClassLevel2" slot="right"></u-switch>
                </u-form-item>

                <u-form-item :label-width="400" label="是否显示秒杀商品" prop="isShowSeckillGoods">
                    <u-switch active-color="red" v-model="validateForm.value.isShowSeckillGoods" slot="right"></u-switch>
                </u-form-item>

                <u-form-item label="商品秒杀活动标题" prop="seckillGoodsTitle">
                    <u-input v-model="validateForm.value.seckillGoodsTitle" placeholder="请输入商品秒杀活动标题"/>
                </u-form-item>

                <u-form-item :label-width="400" label="是否显示商品推荐" prop="isShowGoodsRecommend">
                    <u-switch active-color="red" v-model="validateForm.value.isShowGoodsRecommend" slot="right"></u-switch>
                </u-form-item>
                <u-form-item label="商品推荐标题" prop="goodsRecommendTitle">
                    <u-input v-model="validateForm.value.goodsRecommendTitle" placeholder="请输入商品推荐标题"/>
                </u-form-item>
                <goodsRecommendIndex ref="goodsRecommendIndexRef" :fkId="objParams.id"></goodsRecommendIndex>

                <u-form-item :label-width="400" label="是否显示店铺推荐" prop="isShowStoreRecommend">
                    <u-switch active-color="red" v-model="validateForm.value.isShowStoreRecommend" slot="right"></u-switch>
                </u-form-item>

                <u-form-item label="店铺推荐模块标题" prop="storeRecommendTitle">
                    <u-input v-model="validateForm.value.storeRecommendTitle" placeholder="请输入店铺推荐模块标题"/>
                </u-form-item>
                <storeRecommendIndex ref="storeRecommendIndexRef" :fkId="objParams.id"></storeRecommendIndex>

                <u-form-item label="状态" prop="status" required>
                    <u-input type="select" :select-open="validateForm.controls.status.isShow" v-model="validateForm.controls.status.textDesc"
                             placeholder="请选择状态" @click="validateForm.controls.status.isShow = true; $forceUpdate();"></u-input>
                </u-form-item>
            </u-form>

            <u-button type="error" @click="submit">提交</u-button>
        </div>

        <u-select mode="single-column" label-name="name" value-name="code" :mask-close-able="false"
                  v-model="validateForm.controls.status.isShow" :list="validateForm.controls.status.options"
                  :default-value="validateForm.controls.status.defaultValue"
                  @cancel="validateForm.controls.status.isShow = false; $forceUpdate();"
                  @confirm="UFormUtil.selectSingleColumnConfirm($event, validateForm.controls.status)"></u-select>
    </view>
</template>

<script>

    import {UFormUtil} from "@/dev/core/utils/u-form.util";
    import {SysDictionaryService} from "@/dev/services/sys-dictionary.service";
    import {CommonUtil} from "@/dev/core/utils/common.util";
    import adIndex from '@/pages/common/ad/index.vue'
    import goodsRecommendIndex from '@/pages/common/goods-recommend/index.vue'
    import storeRecommendIndex from '@/pages/common/store-recommend/index.vue'
    import {TbCommunityHomeConfigService} from "@/dev/services/tb-store-sender.service";
    import {LocalStorageUtil} from "@/dev/core/utils/local-storage.util";

    export default {
        data() {
            return {
                fileList: [],
                pickerShow: false,
                codeTips: '',  // 获取验证码提示语
                validateForm: {
                    value: {
                        id: null,
                        communityId: this.globalVariable.userInfo.tbCommunity.id,
                        isShowAd: false,
                        adHeight: 350,
                        isShowSystemClassLevel2: false,
                        isShowSeckillGoods: false,
                        seckillGoodsTitle: '',
                        isShowGoodsRecommend: false,
                        goodsRecommendTitle: '',
                        isShowStoreRecommend: false,
                        storeRecommendTitle: '',
                        status: '1',
                    },
                    rules: {
                        adHeight: [
                            {required: true, message: '必填项!', trigger: ['change', 'blur']},
                            {pattern: /^[0-9]*$/, message: '请输入有效的数值!', trigger: ['change', 'blur']}
                        ],
                        status: [
                            {required: true, message: '必填项!', trigger: ['change', 'blur']},
                            {pattern: /^(?=.*\S).+$/, message: '必填项!', trigger: ['change', 'blur']}
                        ],
                    }
                }
            }
        },
        components: {
            adIndex,
            goodsRecommendIndex,
            storeRecommendIndex
        },
        onReady() {
            const _self = this;
            UFormUtil.setDefaultValues(this.validateForm, this.objParams);
            new SysDictionaryService().optionList('status', null).then(result => {
                UFormUtil.setOptions(_self.validateForm.controls.status, result);
                _self.$forceUpdate();
            });
        },
        methods: {
            submit() {
                const _self = this;
                this.$refs.validateFormRef.validate(valid => {
                    if (valid) {
                        console.log('验证通过', _self.validateForm.value);
                        CommonUtil.alert({
                            title: '提示！',
                            content: '确认提交吗？',
                            showCancel: true, // 是否显示取消按钮
                            success: function (res) {
                                if (res.cancel) { // 点击取消,默认隐藏弹框
                                } else { // 点击确定
                                    new TbCommunityHomeConfigService().updateTbCommunityHomeConfig(_self.validateForm.value).then(result => {
                                        CommonUtil.toastNavigateBack('更新成功');

                                        _self.globalVariable.userInfo.tbCommunityHomeConfig = result;
                                        _self.globalVariable.userInfo.tbCommunityUserList.forEach(item => {
                                            if (item.tbCommunity && item.tbCommunity.tbCommunityHomeConfig && item.tbCommunity.tbCommunityHomeConfig.id === result.id) {
                                                Object.assign(item.tbCommunity.tbCommunityHomeConfig, CommonUtil.deleteNullAttrInObject(result));
                                            }
                                        });
                                        LocalStorageUtil.setItem("userInfo", _self.globalVariable.userInfo);
                                    });
                                }
                            }
                        });
                    } else {
                        console.log('验证失败');
                    }
                });
            }
        }
    }
</script>

<style lang="scss" scoped>
</style>
